package com.chenyi.data.structure.linked;

import java.lang.annotation.ElementType;

/**
 * @author by chenyi
 * @className CircularLinkedList
 * @date 2021/12/25 14:52
 */

public class CircularLinkedList {

    /**
     * 头节点
     */
    private Node first;

    /**
     * 尾节点
     */
    private Node last;

    /**
     * 链表有效长度
     */
    private int size;


    /**
     * 添加
     * @param e
     * @return
     */
    public boolean add(int e) {
        // 判断是否是首次添加
        if (first == null) {
            first = new Node(e, null);
            first.next = first;
            last = first;
        } else {
            last.next = new Node(e, first);
            last = last.next;
        }
        size++;
        return true;
    }

    public boolean add(int e, int index) {
        if (!isElementIndex(index)) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        if (index == 0) {
            Node f = first;
            first = new Node(e, f);
            last.next = first;
        } else {
            // 获取要插入index的上一个节点
            Node prevNode = prevNode(index);
            // 保存要插入index的下一个节点
            Node nextNode = prevNode.next;
            // 将上一个节点连接当前要插入的值，并且连接下一个节点
            prevNode.next = new Node(e, nextNode);
        }
        size++;
        return true;
    }

    public boolean remove(int index) {
        if (!isElementIndex(index)) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        if (index == 0) {
            Node f = first;
            first = f.next;
            last.next = first;
            f = null;
        } else if (index == size - 1) {
            // 将当前元素直接设置为最后一位元素
            last = prevNode(index);;
            // 尾节点的下一个元素指向头节点
            last.next = first;
        } else {
            // 获取当前节点的上一个节点
            Node prevNode = prevNode(index);
            // 保存当前节点
            Node node = prevNode.next;
            // 让当前节点的上一个节点与当前节点的下一个节点连接
            prevNode.next = prevNode.next.next;
            node = null;
        }
        size--;
        return true;
    }

    public void showLinked() {
        Node f = first;
        while (true) {
            System.out.println(f);
            f = f.next;
            if (f == first) {
                break;
            }
        }
    }

    public int getSize() {
        return size;
    }

    /**
     * 遍历获取当前节点上一个node
     * @param index
     * @return
     */
    private Node prevNode(int index) {
        Node node = first;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }

    /**
     * 判断index是否越界
     * @param index
     * @return
     */
    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

    private static class Node {
        private Integer element;
        private Node next;

        public Node(Integer element, Node next) {
            this.element = element;
            this.next = next;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "element=" + element +
                    '}';
        }
    }


}
